package com.glut.campusrepair.controller;

import com.glut.campusrepair.dto.AddEvaluationdto;
import com.glut.campusrepair.dto.MaintenanceShortRecordsdto;
import com.glut.campusrepair.dto.ShowEvaluationdto;
import com.glut.campusrepair.entity.ServiceEvaluation;
import com.glut.campusrepair.service.IServiceEvaluationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 用户评分表 前端控制器
 *
 * @author sanminzhi
 * @since 2024-07-19 11:26
 */
@RestController // 意为当前控制器里面的所有方法都是返回json数据
@RequestMapping("/service-evaluation")
public class ServiceEvaluationController {

    @Autowired
    IServiceEvaluationService iserviceEvaluationService;

    List<ServiceEvaluation> allEvaluation;

    AddEvaluationdto addEvaluationdto;

    // 评价信息
    ShowEvaluationdto showEvaluationdto;



    /**wsy
     * 查看所有的评价
     * @return
     */
    @GetMapping("/getAllEvaluation")
    public List<ServiceEvaluation> selectAllServiceEvaluation() {
        allEvaluation = iserviceEvaluationService.list();

        return allEvaluation;
    }

    /**wsy
     * 通过维修记录id给维修记录添加评价
     *点击评价按钮后的操作
     * */
    @PostMapping("/addEvaluation")
    public String addEvaluation(@RequestBody AddEvaluationdto addEvaluationdto) {

        /*addEvaluationdto.setMaintenanceRecordsId(maintenanceRecordsId);
        addEvaluationdto.setRating(rating);
        addEvaluationdto.setComments(comments);
        addEvaluationdto.setUserNo(userNo);*/
        addEvaluationdto.setEvaTime(LocalDateTime.now());

        boolean result = iserviceEvaluationService.save(addEvaluationdto);
        return result ? "评价成功" : "评价失败";

    }
    /**wsy
     * 通过用户id查询所有简洁版的维修记录，再通过其中的维修记录id查找出相应的评价
     *点击查看评价按钮后的操作
     * */
    @PostMapping("/getEvaluationByUserNo/{userNo}")
    public List<MaintenanceShortRecordsdto> getEvaluationByUserNo(@PathVariable Integer userNo) {
        List<MaintenanceShortRecordsdto> evaluationByUserNo = iserviceEvaluationService.selectAssociation(userNo);
        return evaluationByUserNo;
    }

    /**wsy
     * 点击提交评价按钮后，根据维修记录id将维修记录表（maintenance_records）的评价状态（eva_status）的值改为’已评价‘
     * 已使用
     * */
    @PutMapping("/updateEvaluationStatus/{maintenanceRecordsId}")
    public boolean updateEvaluationStatus(@PathVariable Integer maintenanceRecordsId) {
        boolean b =  iserviceEvaluationService.updateEvaStatus(maintenanceRecordsId);
        return b;
    }

    /**wsy
     * 点击查看评价按钮后，根据维修记录id查询出相应的评价信息
     * */
    @PostMapping("/getEvaluationById/{maintenanceRecordsId}")
    public ShowEvaluationdto getEvaluationById(@PathVariable Integer maintenanceRecordsId) {
         showEvaluationdto = iserviceEvaluationService.selectEvaluationById(maintenanceRecordsId);
        return showEvaluationdto;
    }


    /**
     *
     */
    @GetMapping("/qq")
    public List<ServiceEvaluation> getAllEvaluations1() {
        return iserviceEvaluationService.list();
    }


    @GetMapping("/qq/{id}")
    public ServiceEvaluation getEvaluationById1(@PathVariable Integer id) {
        return iserviceEvaluationService.getById(id);
    }

    @PostMapping("/qq")
    public boolean addEvaluation1(@RequestBody ServiceEvaluation evaluation) {
        return iserviceEvaluationService.save(evaluation);
    }

    @PutMapping("/qq/{id}")
    public boolean updateEvaluation1(@PathVariable Integer id, @RequestBody ServiceEvaluation evaluation) {
        evaluation.setEvaId(id); // 确保 ID 设置到对象中
        return iserviceEvaluationService.updateById(evaluation);
    }
    @DeleteMapping("/{id}")
    public boolean deleteEvaluation1(@PathVariable Integer id) {
        return iserviceEvaluationService.removeById(id);
    }


}
